<?php $this->headScript()
->appendFile($this->js('plugins/jquery.form.js'))
->appendFile($this->js('plugins/jquery.blockUI.js'))
?>
<script>
$(function(){
	// unblock when ajax activity stops
    $(document).ajaxStop($.unblockUI);

	$("#stage").change(function(){
		$("#selectForm").submit();
	});

	$("#subjectSn").change(function(){
		 $("#stage").val('');
         $("#selectForm").submit();
    });

	$("#classSn").change(function(){
		 $("#subjectSn").val('');
	     $("#stage").val('');
	     $("#selectForm").submit();
	});

	$("#yearSemester").change(function(){
		$("#classSn").val('');
	    $("#subjectSn").val('');
	    $("#stage").val('');
	    $("#selectForm").submit();
    });

	$("#scoreForm").ajaxForm({
	    success: function(){
		checkFail();
	    }
		});

    $("#saveBtn").click(function(){
        if ($("#subjectSn").val() =='' || $("#stage").val() == '')
            return false;
    	$.blockUI({ message: '<h1><img src="<?php echo $this->BaseUrl()?>/images/loading.gif" /> <?php echo $this->translate("Just a moment..")?>.</h1>' });
       $("#scoreForm").submit();
     });

	 $(".score").focus(function(){
         $(".score").removeClass('focus');
         $(this).addClass('focus');
         var x = $(this).offset().top - 100; // 100 provides buffer in viewport
         $('html,body').animate({scrollTop: x}, 50);
        });

    // 輸入處理
 $(".score").keydown(function(e){
   var tabIndex =  parseInt($(this).attr('tabindex'));

     switch (e.which) {
   case 9 : //tab
       var score = parseFloat($(this).attr('value'));
       if (isNaN(score)) score = -1;
       if (score < 0 || score > 100) {
           $(this).attr('value','');
           tabIndex --;
       }
       break;
   case 13 : // return
       var score = parseFloat($(this).attr('value'));
       if (isNaN(score)) score = -1;
       if (score < 0 || score > 100) {
           alert($(this).attr('value')+' <?php echo $this->translate('value error')?>');
           $(this).attr('value','');
       }
       else
           tabIndex++;
       break;
   case 40 :
       tabIndex ++;
    break;
   case 38 :
       tabIndex --;
    break;
   }
   if ($(".score[tabindex="+tabIndex+"]").length) {
       $(".score[tabindex="+tabIndex+"]").focus();
   }
//   else
//       $("#signBtn").focus();
});


    $("#score-list table tbody tr:even").addClass('even');
    $("#score-list table tbody tr:odd").addClass('odd');
	// 檢查不及格
	checkFail();
});

function checkFail()
{
    $.each($("#score-list .score"),function(){
        $(this).removeClass('failed');
        var s = $(this).attr('value');
        if (s !='' && s < 60)
              $(this).addClass('failed');
     });
}
</script>
<style>
#score-list table {margin:5px; padding:5px; text-align:center; width:450px;
background: #fed; border-spacing: 1px;}
.failed {color:red}
.focus {background:#ff0}
</style>
<div class="ui-widget">
<form action="" id="selectForm" method="post">
<select name="yearSemester"  id="yearSemester">
<?php echo $this->YearSemesterOptions($this->yearSemester,array('order'=>'year DESC'))?>
</select>
<select id="classSn" name="classSn">
<option value=""><?php echo $this->translate('choose class')?></option>
<?php echo $this->ClassNameOptions($this->yearSemester, $this->classSn)?>
</select>
<?php if ($this->classSn):?>
<select id="subjectSn" name="subjectSn">
<option value=""><?php echo $this->translate('choose subject')?></option>
<?php foreach($this->subjectData['subject'] as $subjectSn=>$val):?>
<option value="<?php echo $subjectSn?>" <?php Sfs_Html::printSelected($subjectSn,$this->subjectSn)?>><?php echo $val['name']?></option>
<?php endforeach?>
</select>
<select id="stage" name="stage">
<option value=""><?php echo $this->translate("choose stage")?></option>
<?php if ($this->subjectData['subject'][$this->subjectSn]['scored_mode'] == 0):?>
<option value="255" <?php Sfs_Html::printSelected(255,$this->stage)?>>
<?php echo $this->translate('whole stage score')?></option>
<?php else:?>
<?php for($i=1; $i<= $this->subjectData['examine']['period']; $i++):?>
<option value="<?php echo $i?>" <?php Sfs_Html::printSelected($i,$this->stage)?>>
<?php echo sprintf($this->translate('%d period'), $i)?></option>
<?php endfor;?>
<?php endif;?>
</select>
<input type="button" id="saveBtn" name="saveBtn" value="<?php echo  $this->translate('save score')?>" />
<?php endif;?>
</form>
<?php if ($this->students):?>
<div id="score-list">
<form method="post" id="scoreForm" action="<?php echo $this->route('set')?>">
<table class="ui-corner-all">
<thead>
<tr>
<th><?php echo $this->translate('student number')?></th>
<th><?php echo $this->translate('student name')?></th>
<?php if ($this->subjectData['subject'][$this->subjectSn]['scored_mode'] == 0):?>
<th><?php echo $this->translate('whole score')?></th>
<?php else:?>
<th><?php echo $this->translate('regular score')?></th>
<th><?php echo $this->translate('usually score')?></th>
<?php endif?>
</tr>
</thead>
<tbody>
<?php $i=0;?>
<?php foreach($this->students as $studentSn=>$val):?>
<tr>
<td><?php echo $val['number']?></td>
<td><?php echo $val['name']?></td>
<?php // 全學期打一次成績?>
<?php if ($this->subjectData['subject'][$this->subjectSn]['scored_mode'] == 0):?>
<td><input  class="score" type="text" size="5" name="score[3][<?php echo $studentSn?>]"  tabindex="<?php echo 200+$i++?>"
 value="<?php if (isset($this->score[$studentSn][Semester_Model_SemesterExamine::WHOLE_SEMESTER_SCORE]))
 echo $this->score[$studentSn][Semester_Model_SemesterExamine::WHOLE_SEMESTER_SCORE]?>"
 />
</td>
<?php else:?>
<td><input   class="score" type="text" size="5" name="score[1][<?php echo $studentSn?>]"  tabindex="<?php echo 200+$i?>"
 value="<?php if (isset($this->score[$studentSn][Semester_Model_SemesterExamine::REGULAR_SCORE]))
 echo $this->score[$studentSn][Semester_Model_SemesterExamine::REGULAR_SCORE]?>"
 />
</td>
<td><input  class="score" type="text" size="5" name="score[2][<?php echo $studentSn?>]" tabindex="<?php echo 300+$i++?>"
 value="<?php if (isset($this->score[$studentSn][Semester_Model_SemesterExamine::USUALLY_SCORE]))
echo $this->score[$studentSn][Semester_Model_SemesterExamine::USUALLY_SCORE]?>"/>
</td>
<?php endif?>
</tr>
<?php endforeach?>
</tbody>
</table>
<input type="hidden"  name="yearSemester"  value="<?php echo $this->yearSemester?>" />
<input type="hidden"  name="classSn"  value="<?php echo $this->classSn?>" />
<input type="hidden"  name="stage"  value="<?php echo $this->stage?>" />
<input type="hidden"  name="subjectSn"  value="<?php echo $this->subjectSn?>"/>
</form>
</div>
<?php endif?>
</div>

